import sys
from time import time
sys.path.insert(0,'../..')
from rmpoly import *
from gmpy import mpz,mpq

try:
  n = int(sys.argv[1])
except:
  print 'prog n'
  sys.exit()

def bench0(N,n):
  rp = RPoly(['x%d' % i for i in range(N)],10, mpz)
  x = rp.gens()
  p = 1 + sum(x)
  t0 = time()
  p1 = p**n
  t1 = time()
  p2 = p.miller_pow(n)
  t2 = time()
  assert p1 == p2
  print '__pow__:%.2f miller_pow:%.2f' % (t1-t0,t2-t1)

def bench1(N,n):
  rp = RPoly(['x%d' % i for i in range(N)],10, mpz)
  x = rp.gens()
  p = 1 + sum(x)
  t1 = time()
  p2 = p.miller_pow(n)
  t2 = time()
  print 'bench1(%d,%d) (1 + x0+...+x%d)^%d t=%.2f' % (N,n,N-1,n,t2-t1)

def bench3():
  rp1 = RPoly(['x'], 8, mpq)
  rp2 = RPoly(['y'], 8, rp1)
  x = rp1.gens()[0]
  y = rp2.gens()[0]
  one2 = rp2('1')
  #p = (x*one2+y)**2
  #p = (x*one2+y)**3
  p = (x*one2+y)**4
  print p

def bench4(N,n):
  rp = RPoly(['x%d' % i for i in range(N)],10, mpz)
  x = rp.gens()
  p = 1 + sum(x)
  t1 = time()
  p2 = p**n
  t2 = time()
  print 'bench4(%d,%d) (1 + x0+...+x%d)^%d t=%.2f' % (N,n,N-1,n,t2-t1)

if n == 0:
  bench0(4,30)
elif n == 1:
  # miller_pow much faster than binary pow
  bench1(4,30)
elif n == 2:
  # binary pow faster than miller_pow
  bench(30,4)
elif n == 3:
  bench3()
elif n ==4:
  bench4(30,4)
else:
  print 'test numbers 0,1,2,3,4 only'
